package com.itheima.controller;


import com.itheima.pojo.Blog;
import com.itheima.service.SortService;
import com.itheima.utils.MessageConstant;
import com.itheima.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/sort")
@Api(tags="播客归档相关接口")
@CrossOrigin
public class SortController {
    @Autowired
    SortService sortService;

    //通过月份获取文章
    @ApiOperation("通过月份获取文章")
    @GetMapping("/getArticleByMonth/{monthDate}")
    public String getArticleByMonth(@PathVariable String monthDate) {
        if (monthDate == null || "".equals(monthDate)) {
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, "请输入月份");
        }
        try {
            List<Blog> blogList = sortService.getArticleByMonth(monthDate);
            return ResultUtil.result(HttpServletResponse.SC_OK, blogList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.GET_ARTICLE_BY_MONTH_FAIL);
        }
    }

    //根据月份归档
    @ApiOperation("根据月份归档")
    @GetMapping("/getSortList")
    public String getSortList(){
        try{
            List<String> monthList=sortService.getSortList();
            return ResultUtil.result(HttpServletResponse.SC_OK,monthList);
        } catch (Exception e){
            e.printStackTrace();
            return ResultUtil.result(HttpServletResponse.SC_BAD_REQUEST, MessageConstant.GET_ARTICLE_BY_MONTH_FAIL);
        }
    }
}
